package com.SparkSQL

import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

object Spark01_SparkSQL_Basic {
  def main(args: Array[String]): Unit = {

    //创建SparkSQL 的运行环境  创建上下文环境配置对象
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL")
    //创建SparkSession对象
    val spark = SparkSession.builder().config(sparkConf).getOrCreate()
    //隐式转换
    import spark.implicits._

    //TODO 执行逻辑操作

    //DataFrame
    //val df: DataFrame = spark.read.json("datas/user.json")
    //df.show()

    //DataFrame => SQL
//    df.createOrReplaceTempView("user")
//
//    spark.sql("select * from user").show
//    spark.sql("select age from user").show
//    spark.sql("select username (age>12) from user").show

    //DataFrame => DSL
    //在使用DataFrame时 ， 如果涉及到转换操作，需要引入转换规则
    import spark.implicits._
//    df.select("age" , "username") .show
//    df.select($"age" + 1).show
//    df.select('age + 2 ) .show


    //DataSet
    //DataFrame 其实是特定泛型的DataSet
    val seq = Seq(1,2,3,4)
//    val ds = seq.toDS()
//    ds.show

    //RDD <=> DataFrame
    val rdd = spark.sparkContext.makeRDD(List((1 , " zhangsan" , 30 ) , ( 2 , "lisi" , 22)) )
    val df: DataFrame = rdd.toDF("id", "name", "age")
    val rowRDD: RDD[Row] = df.rdd


    //DataFrame <=> DataSet
    val ds: Dataset[User] = df.as[User]
    val df1: DataFrame = ds .toDF()

    //RDD <=> DataSet
    val ds1: Dataset[User] = rdd.map {
      case (id, name, age) => {
        User(id, name, age)
      }
    }.toDS()

    val rdd1: RDD[User] = ds1.rdd




    //关闭环境
    spark.close()

  }
  case class User(id:Int , name: String , age: Int)
}
